package com.zjitc.utils;

import com.zjitc.common.redis.MyRedisPool;
import com.zjitc.dao.UserDao;
import java.math.BigInteger;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.UUID;
import redis.clients.jedis.Jedis;

/**
 * Create by IntelliJ IDEA
 *
 * @author: jsonor
 * @date-Time: 2018/3/27 16:08
 * @description:
 */
public class Utils {

  public static String md5Password(String password) {
    try {
      //得到一个信息摘要器
      MessageDigest digest = MessageDigest.getInstance("md5");
      digest.update(password.getBytes());

      return new BigInteger(1, digest.digest()).toString(32);
    } catch (NoSuchAlgorithmException e) {
      e.printStackTrace();
      return null;
    }
  }

  public static String format(Date date) throws ParseException {
    SimpleDateFormat simple = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
    String format = simple.format(date);
    return format;
  }

  public static String getUUid() {
    return UUID.randomUUID().toString();
  }

  public static Long timemistiming(Long date) {
    long now = System.currentTimeMillis();
    long l = (now - date) / (1000 * 60);
    return l;
  }

  public static void setjedis(UserDao dao,MyRedisPool jedis){
    List<String> allusername = dao.findAllusername();
    for (String name : allusername) {
      jedis.addSet("username", name);
    }

    List<String> allemail = dao.findAllEmail();
    for (String email : allemail) {
      jedis.addSet("email", email);
    }
  }
}
